package com.benny_driving.http.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.benny_driving.constant.HttpActionConstant;
import com.benny_driving.constant.URLConstant;
import com.benny_driving.entity.DriLoc;
import com.benny_driving.http.dao.DriLocDao;
import com.benny_driving.net.NetTool;

public class DriLocDaoImpl implements DriLocDao {

	@Override
	public List<DriLoc> getDriversLoc(double log, double lat) {
		String url = URLConstant.BASE_URL + URLConstant.LOC_SHARE_SERVLET;
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("action",
				HttpActionConstant.GET_DRIVERS_LOC));
		params.add(new BasicNameValuePair("log", log + ""));
		params.add(new BasicNameValuePair("lat", lat + ""));
		List<DriLoc> driLocs = new ArrayList<DriLoc>();
		try {
			String resData = NetTool.sendNameValuePostRequest(url, params);
			Log.i("httpTest", resData);
			JSONObject jsonObject = new JSONObject(resData);
			JSONArray jsonArray = jsonObject.getJSONArray("drilocs");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject json = (JSONObject) jsonArray.opt(i);
				DriLoc driLoc = new DriLoc(json.getDouble("log") + "",
						json.getDouble("lat") + "", json.getString("driid")
								+ "", json.getInt("stars") + "",
						json.getDouble("dis") + "", json.getInt("state") + "");
				driLocs.add(driLoc);
			}
			return driLocs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		// Map<String, String> params = null;
		// List<DriLoc> driLocs = null;
		// params = new HashMap<String, String>();
		// params.put("action", HttpActionConstant.GET_DRIVERS_LOC);
		// params.put("longitude", longitude + "");
		// params.put("latitude", latitude + "");
		// String resData = NetTool.sendGetRequest(URLConstant.BASE_URL,
		// params);
		// Log.i("junit", "resData: " + resData);
		// resJsonArr = new JSONObject(resData).getJSONArray("drilocs");
		// driLocs = new ArrayList<DriLoc>();
		// for (int i = 0; i < resJsonArr.length(); i++) {
		// JSONObject resJson = null;
		// DriLoc driLoc = null;
		// resJson = (JSONObject) resJsonArr.opt(i);
		// driLoc = new DriLoc(Integer.parseInt(resJson.getString("log")),
		// Integer.parseInt(resJson.getString("lat")),
		// resJson.getString("driid"), resJson.getInt("stars"),
		// resJson.getString("dis"));
		// driLocs.add(driLoc);
		// }

	}
}
